<template>

  <div class="app-container">

    <el-form :inline="true" :model="queryForm" class="demo-form-inline" >
<!--      <el-form-item style="margin-bottom:10px;">
        <el-input v-model="queryForm.keywords" prefix-icon="el-icon-search" placeholder="服务名" style="width: 18.75rem;" />
      </el-form-item> -->
      <el-form-item style="margin-bottom:10px;">
        <el-button type="primary" icon="el-icon-plus"  :loading="loading" @click="createSignalingServer" plain>信令服务</el-button>
        <el-button type="primary" icon="el-icon-plus"   :loading="loading" @click="createRelayServer" plain>中继服务</el-button>
        <!-- <el-button type="primary" icon="el-icon-plus"  size="mini" :loading="loading" @click="createRelayServer">Nginx</el-button> -->
      </el-form-item>
    </el-form>
    <el-table
      :data="tableData"
      border
      empty-text="暂无数据."
      style="width: 100%">
      <el-table-column
        type="selection"
        width="40">
      </el-table-column>
      <el-table-column
            type="index"
            label="序号"
            width="50"
            align="center"
            :index="indexMethod">
          </el-table-column>

      <el-table-column
        prop="name"
        label="名称"
        >
      </el-table-column>

      <el-table-column
        prop="type"
        label="类型"
        >
      </el-table-column>


      <el-table-column prop="enabled" label="是否启用"  align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.enabled='1' " >启用</el-tag>
          <el-tag v-else type="danger">禁用</el-tag>
        </template>
      </el-table-column>


      <el-table-column
        prop="status"
        label="状态"
        >
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status=='-1' "  type="success">不可用</el-tag>
          <el-tag v-else-if="scope.row.status=='0' "  type="danger">停止</el-tag>
          <el-tag v-else-if="scope.row.status=='1' "  type="success">正在启动</el-tag>
          <el-tag v-else-if="scope.row.status=='2' "  type="success">运行中</el-tag>
          <el-tag v-else-if="scope.row.status=='3' "  type="success">停止中</el-tag>
          <el-tag v-else type="danger">未知</el-tag>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="left" >
       <template slot-scope="scope">
         <el-button type="text" icon="el-icon-edit-outline" size="small" @click="handleEdit(scope.row)">编辑
         </el-button>


         <el-button v-if="scope.row.status=='0'"  type="text" icon="el-icon-refresh-right" @click="handleStart(scope.row)">启动
         </el-button>

         <el-button v-if="scope.row.status=='2'"  type="text" icon="el-icon-refresh-right" @click="handleStop(scope.row)">停止
         </el-button>

         <el-button  type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除
         </el-button>

       </template>
      </el-table-column>
    </el-table>

    <el-row>
      <el-col :span="24">
        <div align="right" style="margin-top: 0.3125rem;">
          <el-pagination background layout="prev, pager, next" :current-page="page.cur" :page-size="page.limit" :total="page.total" @current-change="handleCurrentChange" />
        </div>
      </el-col>
    </el-row>

    <!--编辑框-->
    <info-dialog
      v-if="editDialog.visible"
      :properties="editDialog"
      @closed="close"
      @refresh="load"
    />

    <SignalingDialog
      v-if="signalingDialog.visible"
      :properties="signalingDialog"
      @closed="closeSignaling"
      @refresh="load"
    />

    <RelayDialog
      v-if="relayDialog.visible"
      :properties="relayDialog"
      @closed="closeRelay"
      @refresh="load"
    />

    <NginxDialog
      v-if="nginxDialog.visible"
      :properties="nginxDialog"
      @closed="closeNginx"
      @refresh="load"
    />




  </div>
</template>

<script>
  import InfoDialog from './components/InfoDialog.vue'
  import SignalingDialog from './components/SignalingDialog.vue'
  import RelayDialog from './components/RelayDialog.vue'
  import NginxDialog from './components/NginxDialog.vue'




  import {
    hasPermission
  } from '@/utils/permission'
  import {
    findServerList,
    createSignalingServer,
    createRelayServer,
    deleteServer,
    restartServer,
    stopServer
  } from '@/api/cloud/server'
  import {
    isOk
  } from '@/utils/request'
  export default {
    name:'Project',
    components: {
      InfoDialog,
      SignalingDialog,
      RelayDialog,
    },
    data() {
      return {
        loading:false,
        queryForm: {
          keywords: undefined,
          status: '',
          orgId: ''
        },
        page: {
          cur: 1,
          limit: 50,
          total: 0
        },
        tableData: [],
        editDialog: {
          visible: false,
          type: '0',
          data: {}
        },
        signalingDialog: {
          visible: false,
          type: '0',
          data: {}
        },
        relayDialog: {
          visible: false,
          type: '0',
          data: {}
        },
        nginxDialog: {
          visible: false,
          type: '0',
          data: {}
        },

      }
    },
    created() {
      this.load();
    },
    methods: {
      indexMethod(index) {
        return index +1;
      },
      onQuerySubmit(event) {
        this.load()
      },
      onReset(event) {
        this.queryForm.keywords=undefined;
        this.queryForm.status='';
        this.load()
      },
      load(){
        this.loading=true
        findServerList({
          cur: this.page.cur,
          limit: this.page.limit,
          keywords: this.queryForm.keywords,
        }).then(rsp => {
          if(isOk(rsp)){
            const _data = rsp.result
            this.page.total = _data.totalRow
            this.tableData = _data.list
            this.loading = false
          }
        }).catch(err => {
          console.log(err);
        })
        .finally(() => {
          this.loading = false
        })
      },
      close: function(e) {
        this.editDialog.visible = false
        this.editDialog.data = {}
        this.load()
      },
      handleCurrentChange(cur) {
        this.page.cur = cur
        this.load()
      },
      handleAdd: function(e) {
        this.editDialog.visible = true
        this.editDialog.type = '0'
      },
      handleDelete: function(data) {
        this.$confirm('若服务正在运行，将自动停止，确认删除【' + data.name + '】吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          deleteServer({
            id: data.id
          }).then(response => {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 500
            })
            this.load()
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
        })
      },
      handleStart: function(data) {
          this.loading = true
          restartServer({
            id: data.id
          }).then(response => {
            this.$message({
              message: '成功',
              type: 'success',
              duration: 500
            })
            this.load()
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
      },
      handleStop: function(data) {
        this.$confirm('重启服务，将会影响正在使用该服务的用户，确认重启【' + data.name + '】吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          stopServer({
            id: data.id
          }).then(response => {
            this.$message({
              message: '成功',
              type: 'success',
              duration: 500
            })
            this.load()
          }).catch(err => {
            console.log(err);
          })
          .finally(() => {
            this.loading = false
          })
        })
      },
      handleEdit: function(data) {
        if('signaling'==data.type){
          this.handleEditSignaling(data);
        }
        else if('relay'==data.type){
          this.handleEditRelay(data);
        }
      },
      createSignalingServer(){
        this.loading=true
        createSignalingServer({
        }).then(rsp => {
          if(isOk(rsp)){
            this.$message({
              type: 'success',
              message:rsp.message
            })
            this.load();
          }
          this.loading = false
        }).catch(err => {
          console.log(err);
        })
        .finally(() => {
          this.loading = false
        })
      },
      createRelayServer(){
        this.loading=true
        createRelayServer({
        }).then(rsp => {
          if(isOk(rsp)){
            this.$message({
              type: 'success',
              message:rsp.message
            })
            this.load();
          }
          this.loading = false
        }).catch(err => {
          console.log(err);
        })
        .finally(() => {
          this.loading = false
        })
      },
      handleEditSignaling: function(data) {
        this.signalingDialog.visible = true
        this.signalingDialog.type = '1'
        this.signalingDialog.data = data
      },
      closeSignaling: function(e) {
        this.signalingDialog.visible = false
        this.signalingDialog.data = {}
        this.load()
      },
      handleEditRelay: function(data) {
        this.relayDialog.visible = true
        this.relayDialog.type = '1'
        this.relayDialog.data = data
      },
      closeRelay: function(e) {
        this.relayDialog.visible = false
        this.relayDialog.data = {}
        this.load()
      },
      handleEditNginx: function(data) {
        this.nginxDialog.visible = true
        this.nginxDialog.type = '1'
        this.nginxDialog.data = data
      },
      closeNginx: function(e) {
        this.nginxDialog.visible = false
        this.nginxDialog.data = {}
        this.load()
      },
    }
  }
</script>
